<!--
  - Copyright(c)  2022 厦门外里科技有限公司 All rights reserved.
  -
  - https://www.wailikeji.com
  -
  - 版权所有，侵权必究！
  -->

<template>
	<view class="panel">
		<view class="panel-content">
			<view class="wrap">
				<view v-if="user.passwordIsNull" class="remind-text">您还未设置登录密码<br>
					请设置登录密码！</view>
				<view v-else class="remind-text">请设置登录密码<br>
					定期更新密码提高安全性</view>
				<u-form class="form" :model="model" :rules="rules" ref="uForm">
					<u-form-item v-if="!user.passwordIsNull" label="旧密码" prop="oldPassword" label-width="180">
						<u-input type="password" v-model="model.oldPassword" placeholder="请输入旧密码"></u-input>
					</u-form-item>
					<u-form-item label="新密码" prop="newPassword" label-width="180">
						<u-input type="password" v-model="model.newPassword" placeholder="请输入新密码"></u-input>
					</u-form-item>
					<u-form-item label="确认密码" prop="confirmPassword" label-width="180">
						<u-input type="password" v-model="model.confirmPassword" placeholder="请确认新密码"></u-input>
					</u-form-item>
				</u-form>
				<view class="remind-text">
					<u-icon name="question-circle" color="#2767dc" size="28"></u-icon>
					密码必须是8-16位的数字，字符组合（不能是纯数字）
				</view>
				<view class="form-footer">
					<u-button class="btn" type="primary" @click="submit">提交</u-button>
					<!-- <u-button class="btn" type="default" @click="cancel">关闭</u-button> -->
				</view>
			</view>
		</view>
	</view>
</template>
<script>
	/**
	 * Copyright (c) 2013-Now http://aidex.vip All rights reserved.
	 */
	// import base64 from '@/common/base64.js';
	let _this;
	export default {
		data() {
			return {
				model: {
					oldPassword: '',
					newPassword: '',
					confirmNewPassword: ''
				},
				user: this.$store.state.user.userInfo,
			};
		},
		mounted() {
			_this = this;
		},
		computed: {
			rules() {
				return {
					oldPassword: [{
						required: !this.user.passwordIsNull,
						message: '请输入旧密码',
						trigger: ['change', 'blur'],
					}],
					newPassword: [{
							required: true,
							message: '请输入新密码',
							trigger: ['change', 'blur'],
						},
						{
							pattern: /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]+\S{5,12}$/,
							message: '需同时含有字母和数字，长度在6-12之间',
							trigger: ['change', 'blur'],
						}
					],
					confirmPassword: [{
							required: true,
							message: '请重新输入密码',
							trigger: ['change', 'blur'],
						},
						{
							validator: (rule, value, callback) => {
								return value === this.model.newPassword;
							},
							message: '两次输入的密码不相等',
							trigger: ['change', 'blur'],
						}
					],
				}
			}
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules);
		},
		methods: {
			submit() {
				console.log("提交")
				this.$refs.uForm.validate(valid => {
					console.log("提交", valid)
					if (!valid) {
						this.$u.toast('您填写的信息有误，请根据提示修正。');
						return
					}
					this.$api.api('user.changePassword', {
							"password": _this.model.oldPassword,
							"newPassword": _this.model.newPassword
						})
						.then(res => {
							if (res.code == 200) {
								uni.showToast({
									icon: 'none',
									position: 'bottom',
									title: '修改成功！'
								});
							} else {
								uni.showToast({
									icon: 'none',
									position: 'bottom',
									title: res.msg
								});
							}
						})

				});
			},
			cancel() {
				uni.navigateBack();
			}
		}
	};
</script>
<style lang="scss">
	@import '../../components/my/aidex.scss';

	page {
		background: #f5f5f5;
	}

	.u-form {
		background: #fff;
		padding: 0px 15px;
	}

	.remind-text {
		padding: 20rpx 30rpx;
		color: #666666;
	}


	.panel {
		padding: 0 30upx 0;
		// color: black;

		.panel-content {
			background: white;
			margin: 0 auto;
			border-radius: 16upx; //面包圆形倒角
			box-shadow: 0 2upx 20upx rgba(183, 183, 183, 0.3); //周边阴影
			margin-top: 16upx; //顶部间隔
		}
	}
</style>
